package org.mobile.servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.mobile.beans.User;
import org.mobile.logic.UserLogic;

/**
 * Servlet implementation class EditUser
 */
@WebServlet(name = "editUser", urlPatterns = { "/editUser" })
public class EditUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User sessionUser= (User)request.getSession().getAttribute("user");
		String name=  new String(request.getParameter("name").getBytes("ISO8859_1"), "UTF-8");
		String surname= new String(request.getParameter("surname").getBytes("ISO8859_1"), "UTF-8");
		String address= new String(request.getParameter("address").getBytes("ISO8859_1"), "UTF-8");
		sessionUser.setName(name);
		sessionUser.setSurname(surname);
		sessionUser.setAddress(address);
		if(!UserLogic.getInstance().updateUser(sessionUser)){
			request.setAttribute("error", "Bład zmiany danych!");
			request.getRequestDispatcher("errPage.jsp").forward(request, response);
		}
		else{
			request.setAttribute("message", "Dane zostały zmienione");
			request.getRequestDispatcher("index.jsp").forward(request, response);				
		}

		
	}

}
